package com.bigflexlib.x.mx.collections
{
	
	
	/**
	 * 
	 * A simple utility class that represents the positional aspects of a slice.
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class Slice
	{
		
		public var length:uint;
		
		public var startIndex:uint;
		
		public var endIndex:uint;
		
		
		
		public function Slice( startIndex:uint, length:uint )
		{
			this.startIndex = startIndex;
			this.length = length;
			
			// derive this one
			this.endIndex = startIndex + length;
		}
		
		
		private var _offsetLimitString:String;
		public function get offsetLimitString( ):String
		{
			if ( _offsetLimitString == null )
				_offsetLimitString = "OFFSET " + startIndex + " LIMIT " + length;
			return _offsetLimitString;
		}		
		
		
		
		/**
		 * 
		 * @param startIndex
		 * 		= offset
		 * 
		 * @param length
		 * 		= limit
		 * 		= pageSize
		 * @return 
		 * 
		 */		
		public static function forStartAndLength( startIndex:uint, length:uint ):Slice
		{
			return forStartAndEnd( startIndex, startIndex + length );	
		}
		
		
		public static function forStartAndEnd( startIndex:uint, endIndex:uint ):Slice
		{
			return new Slice( startIndex, endIndex );		
		}
		
		public static function forPage( pageNumber:uint, pageSize:uint ):Slice
		{
			return forStartAndLength( pageNumber * pageSize, pageSize );		
		}
		
	}
}